/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#ifndef __COMMON_FIO_H
#define __COMMON_FIO_H

#include <stdio.h>
#include "data.h"

#define Z_FIO_WHOLE -1
#define Z_FIO_BUFSIZE_DEFAULT 1024

//struct z_freader: IO stream for file reading
typedef struct
{
	char *path; //memberof z_freader: char *path: path of the file
	FILE *handle; //memberof z_freader: FILE *handle: file descriptor
	z_size length; //memberof z_freader: z_size length: length of the file (in bytes)
	char *buffer; //memberof z_freader: char *buffer: IO buffer
	z_size bufsize; //memberof z_freader: z_size bufsize: allocated sizeof buffer
} z_freader;

z_freader z_freader_init(char *path, int bufsize); //methodof z_freader: initializer
//argof z_freader_init: char *path: path of the file to read
//argof z_freader_init: int bufsize: can be Z_FIO_WHOLE to read the whole file, Z_FIO_BUFSIZE_DEFAULT (1024), or any other size
z_freader *z_freader_new(char *path, int bufsize); //methodof z_freader: allocator
int z_freader_release(z_freader f); //methodof z_freader: uninitializer
int z_freader_delete(z_freader *f); //methodof z_freader: deletor
int z_freader_ok(z_freader *f); //methodof z_freader: checks to make sure the IO stream is good-to-go
z_data z_freader_read(z_freader *f); //methodof z_freader: read a buffer-full of data from the file
z_data *_z_freader_read(z_freader *f);
z_size z_freader_left(z_freader *f); //methodof z_freader: returns how many bytes are left unread in the stream

/// {{z_freader reader = z_freader_init("thisIsAFile", Z_FIO_WHOLE);
/// if (!z_freader_ok(&reader))
/// // this is when the file didn't open correctly (file doesn't exist, maybe)
///
/// z_data buf = z_freader_read(&reader);
/// printf("%s\n", (char *)z_data_src_get(&buf));
///
/// z_freader_release(reader);}}

#endif
